from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


class BaseApi:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, loc):
        element = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(loc))
        return element

    def find_elements(self, loc, num):
        element = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(loc))
        return element[num]

    def find_elementss(self,loc):
        element=WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((loc)))
        return element

    def click_element(self, loc):
        self.find_element(loc).click()

    def click_elements(self, loc, num):
        self.find_elements(loc, num).click()

    def switch_window(self):
        win = self.driver.window_handles
        self.driver.switch_to.window(win[-1])

    def input_text(self, loc, text):
        ele = self.find_element(loc)
        ele.clear()
        ele.send_keys(text)

    def inputs_text(self,loc,num,text):
        ele = self.find_elements(loc,num)
        ele.clear()
        ele.send_keys(text)